using Castle.Core.Internal;
using Confluent.Kafka;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SkiaSharp;
using StackExchange.Redis;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.eager;

namespace VOL.Finance.Controllers
{
    [Route("api/cellSynchronization")]
    [ApiController]
    public partial class cellSynchronizationController : Controller
    {
        /// <summary>
        /// 该任务是执行同步溯源平台上的电池包数据置117库上
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("cellSynchronization"), AllowAnonymous]
        public dynamic synchronization()
        {
            List<dynamic> rntlist1 = new List<dynamic>();
            
            string create_time = DateTime.Now.ToString("yyyy-MM");
           List<dynamic> rntlist = MySql_Helper.query_common("SELECT code,factory_system from sys_battery_pack_entity_storage where create_time >= '"+create_time+"'  ORDER BY factory_system ", "31");//读取mysql

            //List<dynamic> rntlist = MySql_Helper.query_common("SELECT code,factory_system from sys_battery_pack_entity_storage  ORDER BY factory_system ", "31");//读取mysql
            foreach (var item in rntlist)
            {
                item.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                rntlist1.Add(item);
            }

            List<tempProperty> ybbom_list = null;
            if (rntlist1.Count > 0)
            {
                ybbom_list = Other_Oracle_Helper.query<tempProperty>("SELECT code,factory_system from BATTERY_CODE", "117");
            }

            // 该步骤是将从溯源平台上的电池数据及117库上的数据对比，保存未录入的电池溯源数据至117库上
            foreach (var item in rntlist1)
            {
                foreach (var item1 in ybbom_list)
                {
                    if (item1.code.Equals(item.code))
                    {
                        rntlist.Remove(item);
                    }
                }
            }

            string temp = "0";
            for (int i = 0; i < rntlist.Count; i++)
            {
                Other_Oracle_Helper.Execute("insert into BATTERY_CODE(code,factory_system,create_time,PFLAGE) values('" + rntlist[i].code+"','"+ rntlist[i].factory_system + "','" + rntlist[i].create_time + "','" + temp + "')", "117");//插入oracle
            }
            
            return rntlist;
        }



        public class tempProperty { 
            public string code { get; set; } //电池包编号
            public string factory_system { get; set; } //框架编号
            public string create_time { get; set; } //创建时间
        }

        public class tempMesProperty
        {
            public string PACKNO { get; set; } //电池包编号
            public string SSCNZZBM { get; set; } //框架编号
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("cellSynchronizationMES"), AllowAnonymous]
        public dynamic cellSynchronization()
        {
            List<dynamic> rntlist1 = new List<dynamic>();
            string create_time = DateTime.Now.ToString("yyyy-MM");
            // 从117库上获取未同步数据
            List<tempProperty> rntlist = Other_Oracle_Helper.query<tempProperty>("SELECT code,factory_system from BATTERY_CODE WHERE PFLAGE = '0' and create_time >= '" + create_time + "'", "117");
            // List<tempProperty> rntlist = Other_Oracle_Helper.query<tempProperty>("SELECT code,factory_system from BATTERY_CODE WHERE PFLAGE = '0' ", "117");
            foreach (var item in rntlist)
            {
                
                item.create_time = DateTime.Now.ToString("yyyyMMddHHmmss");
                rntlist1.Add(item);
            }
            // 从mes数据库上获取数据。用于判断哪些数据需要插入到mes库中
            List<tempMesProperty> mes_list = Other_Oracle_Helper.query<tempMesProperty>("select PACKNO,SSCNZZBM from CELLPACKINFO", "mes");
            foreach (var item in rntlist1)
            {
                foreach (var item1 in mes_list)
                {
                    if (item1.PACKNO.Equals(item.code))
                    {
                        rntlist.Remove(item);
                    }
                }
            }
            
            //插入到mes库中
            for (int i = 0; i < rntlist.Count; i++)
            {
                Console.WriteLine(rntlist[i].code);
                Other_Oracle_Helper.Execute("insert into CELLPACKINFO(PACKNO,SSCNZZBM,IMPORTTIME,IMPORTMAN) values('" + rntlist[i].code + "','" + rntlist[i].factory_system + "','" + rntlist[i].create_time + "','" + "SYSTEM" + "')", "mes");//插入oracle
            }

            // 反更117表中的同步字段
            for (int i = 0; i < rntlist.Count; i++)
            {
                Other_Oracle_Helper.Execute("update BATTERY_CODE set PFLAGE = '1' where code =  '" + rntlist[i].code + "'", "117");//插入oracle
            }

            return rntlist.Count;
        }
    }
}

